﻿using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class modelScene : MonoBehaviour
{
    /// <summary>
    /// This script should be set for cardboard camera.
    /// It can control the camera, buttons, the flying boundary, the properties of flying of the head
    /// </summary>
    public bool isMove { set; get; }
    public float speed = 1f;
    public string QuitScene;
    // Use this for initialization
    void Start()
    {
        isMove = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (isMove)
        {
            BoundaryHead();
            MoveHead(speed);
        }
    }

    void MoveHead(float speed)
    {
        // Move the head
        GameObject head = GameObject.FindGameObjectWithTag("head");
        GameObject camera = GameObject.FindGameObjectWithTag("camera");
        Vector3 positionCamera = new Vector3();
        Vector3 rotationHead = new Vector3();
        positionCamera = camera.GetComponent<Transform>().position;
        rotationHead = head.GetComponent<Transform>().localRotation.eulerAngles;

        if (rotationHead.y >= 0 && rotationHead.y < 45)
        {
            positionCamera.z += Time.deltaTime * speed;
        }
        else if (rotationHead.y >= 45 && rotationHead.y < 90)
        {
            positionCamera.x += Time.deltaTime * speed;
        }
        else if (rotationHead.y >= 90 && rotationHead.y < 135)
        {
            positionCamera.x += Time.deltaTime * speed;
        }
        else if (rotationHead.y >= 135 && rotationHead.y < 180)
        {
            positionCamera.z -= Time.deltaTime * speed;
        }
        else if (rotationHead.y >= 180 && rotationHead.y < 225)
        {
            positionCamera.z -= Time.deltaTime * speed;
        }
        else if (rotationHead.y >= 225 && rotationHead.y < 270)
        {
            positionCamera.x -= Time.deltaTime * speed;
        }
        else if (rotationHead.y >= 270 && rotationHead.y < 315)
        {
            positionCamera.x -= Time.deltaTime * speed;
        }
        else if (rotationHead.y >= 315 && rotationHead.y < 360)
        {
            positionCamera.z += Time.deltaTime * speed;
        }

        if (rotationHead.x >= 30 && rotationHead.x <= 90)
        {
            positionCamera.y -= (Time.deltaTime * speed + rotationHead.x / 1000);
        }
        else if (rotationHead.x <= 330 && rotationHead.x >= 270)
        {
            positionCamera.y += (Time.deltaTime * speed + (360 - rotationHead.x) / 1000);
        }
        camera.GetComponent<Transform>().position = positionCamera;
    }

    void BoundaryHead()
    {
        // Set the boundary so that user can not fly out of the map
        GameObject head = GameObject.FindGameObjectWithTag("head");
        GameObject camera = GameObject.FindGameObjectWithTag("camera");
        Vector3 position = new Vector3();
        position = camera.GetComponent<Transform>().position;

        if (position.y >= 100) position.y = 100;
        else if (position.y <= 4) position.y = 4;

        if (position.z >= 190) position.z = 190;
        else if (position.z <= -60) position.y = -60;

        if (position.x >= 160) position.x = 160;
        else if (position.x <= -160) position.x = -160;

        camera.GetComponent<Transform>().position = position;
    }

    public void AddSpeed()
    {
        this.speed++;
        this.speed++;
        if (speed >= 10) speed = 10;
    }

    public void MinusSpeed()
    {
        this.speed--;
        this.speed--;
        if (speed <= 0) speed = 0;
    }

    public void Reset()
    {
        this.GetComponent<Transform>().position = new Vector3(0, 4, 0);
    }

    public void Quit()
    {
        SceneManager.LoadScene(QuitScene);
    }

    public void VRmode()
    {
        Cardboard.SDK.VRModeEnabled = !Cardboard.SDK.VRModeEnabled;
    }

    public void MessageButton()
    {
        // Set the start position of the button
        GameObject.Find("messageButton").GetComponent<RectTransform>().localPosition = new Vector3(0, -200, -700);
    }
}
